package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.LightContract;
import com.sunlg.smarthome.data.entity.Light;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.utils.PreferHelper;

import java.util.List;

/**
 * Created by sunlg on 2017/4/20.
 */

public class LightPresenter implements LightContract.Presenter {

    private LightContract.View mView;
    private SHServiceWrapper mSHServiceWrapper;
    private PreferHelper mPreferHelper;

    public LightPresenter() {
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
        mPreferHelper = new PreferHelper(App.getInstance(), "gateway");
    }

    @Override
    public void takeOn(int lightId, int level) {
        mSHServiceWrapper.takeOnLight(lightId, level, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.takeOnSuccess();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void takeOff(int lightId) {
        mSHServiceWrapper.takeOff(lightId, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.takeOffSuccess();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void loadAll() {
        final LightContract.LightListView view = (LightContract.LightListView)mView;
        mSHServiceWrapper.loadAllLight(mPreferHelper.getInt("id", 0),
                new MySubscriber<Result<List<Light>>>() {
            @Override
            public void onSuccess(Result<List<Light>> data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    List<Light> lightList = data.getData();
                    if(lightList == null && lightList.size() <= 0){
                        view.showNoneLightText();
                    }else {
                        view.hideNoneLightText();
                        view.showLightList(data.getData());
                    }
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                view.showError(e.getMessage());
            }
        });
    }

    @Override
    public void addLight(Light light) {
        light.setGatewayId(mPreferHelper.getInt("id", -1));
        mSHServiceWrapper.addLight(light, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    loadAll();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void deleteLight(int lightId) {
        mSHServiceWrapper.deleteLight(lightId, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    loadAll();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void attachView(LightContract.View view) {
        this.mView = view;
    }

    @Override
    public void dettachView() {
        this.mView = null;
    }
}
